﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace QuantumQuest
{
    public partial class QQBody : UserControl
    {
        
        public QQVector2 _position;
        protected QQVector2 _velocity;
        protected QQVector2 _acceleration;
        protected double _mass;
        protected int _zIndex;
        protected bool _isStationary;
        protected bool _isConstantAccel;
        protected QQVector2 _heading;

        protected TransformGroup _transGroup;
        protected RotateTransform _rotate;
        protected ScaleTransform _scale;


        public QQBody(QQVector2 position, QQVector2 velocity, QQVector2 accleration, 
                      double mass, int zIndex,bool isStationary, bool isConstantAccel)
        {
            
            InitializeComponent();
            _position = position;
            _velocity = velocity;
            _acceleration = accleration;
            _heading.X = _velocity.X;
            _heading.Y = _velocity.Y;
            _heading.Normalize();
            _zIndex = zIndex;

            _mass = mass;
            _isStationary = isStationary;
            _isConstantAccel = isConstantAccel;

            UpdateBody();
            
        }
        public QQBody()
        {
            InitializeComponent();
            UpdateBody();
        }

        public QQVector2 Position
        {
            get
            {
                return _position;
            }
            set
            {

                _position = value;
                UpdateBody();

            }
        }
        public QQVector2 Velocity
        {
            get
            {
                return _velocity;
            }
            set
            {

                _velocity = value;

            }
        }
        public QQVector2 Acceleration
        {
            get
            {
                return _acceleration;
            }
            set
            {

                _acceleration = value;

            }
        }
        public double Mass
        {
            get
            {
                return _mass;
            }
            set
            {

                _mass = value;

            }
        }
        public int ZIndex
        {
            get
            {
                return _zIndex;
            }
            set
            {
                _zIndex = value;
            }
        }
        public ScaleTransform ScaleTrans
        {
            get
            {
                return _scale;
            }
            set
            {
                _scale = value;
            }
        }
        public QQVector2 Heading
        {
            get
            {
                return _heading;
            }
            set
            {

                _heading = value;
                _heading.Normalize();
                UpdateBody();
            }
        }
        public bool IsStationary
        {
            get
            {
                return _isStationary;
            }
            set
            {
                _isStationary = value;
            }
        }
        public bool IsConstantAccel
        {
            get
            {
                return _isConstantAccel;
            }
            set
            {
                _isConstantAccel = value;
            }
        }
        public void RotateCounterClockWRad(double drad)
        {
            _heading.ThetaRad += drad;
            UpdateBody();
        }
        public void RotateCounterClockWDeg(double ddeg)
        {
            _heading.ThetaDeg += ddeg;
            UpdateBody();
        }
        public void RotateClockWRad(double drad)
        {
            RotateCounterClockWRad(((2.0) * (Math.PI)) - drad);
            UpdateBody();
        }
        public void RotateClockWDeg(double ddeg)
        {
            RotateCounterClockWDeg(360 - ddeg);
            UpdateBody();
        }

        public void UpdateBody()
        {
            //so when we change a transformation, do we have to run through this again, or is it taken care of?
            this.RenderTransformOrigin = new Point(0.5, 0.5);//transform about the center of the planet

            _transGroup = new TransformGroup();
           
            _rotate = new RotateTransform();
            _scale = new ScaleTransform();

            _rotate.Angle = 360 - _heading.ThetaDeg;//so it says it wants it in degrees clockwise...so this should work?
            
            
            //_translate.X = _position.X;//WRONG WRONG WRONG; needs to be relative to some point
            //_translate.X = _position.Y;//likewise
            this.SetValue(Canvas.LeftProperty, _position.X);
            this.SetValue(Canvas.TopProperty, _position.Y);
            this.SetValue(Canvas.ZIndexProperty, _zIndex);

            
            _transGroup.Children.Add(_rotate);
            _transGroup.Children.Add(_scale);

            this.RenderTransform = _transGroup;

        }

    }
}
